package cn.zhiwen.config;

import cn.zhiwen.entity.datasource.DataSources;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import rx.internal.util.LinkedArrayList;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DynamicDataSource extends AbstractRoutingDataSource {
    /**
     * 每次请求动态请求哪一个数据源
     * @return
     */
    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceHolder.getDataSource();
    }



    public DynamicDataSource(){
        List<DataSources> sourcesArrayList = new LinkedList<DataSources>();
        //ANJICRM
        DataSources dataSources1 = new DataSources();
//        dataSources1.setUrl("jdbc:mysql://10.108.2.26:3306/ANJICRM?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=gbk&useSSL=false");
//        dataSources1.setUsername("test");
//        dataSources1.setPassword("test");
        dataSources1.setUrl("jdbc:mysql://10.108.141.199:3306/ANJICRM?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=gbk&useSSL=false");
        dataSources1.setUsername("crm_uat");
        dataSources1.setPassword("anjiplus#crm^$uat$^2018.com!#mysql");
        DataSources dataSources2 = new DataSources();
        //ANJIAUTH
//        dataSources2.setUrl("jdbc:mysql://10.108.2.26:3306/ANJIAUTH?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8");
//        dataSources2.setUsername("test");
//        dataSources2.setPassword("test");
        dataSources2.setUrl("jdbc:mysql://10.108.141.199:3306/crm_auth?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=gbk&useSSL=false");
        dataSources2.setUsername("crm_uat");
        dataSources2.setPassword("anjiplus#crm^$uat$^2018.com!#mysql");
        sourcesArrayList.add(dataSources1);
        sourcesArrayList.add(dataSources2);
        Map<Object, Object> dataSources=new ConcurrentHashMap<>();
        for(int i=0;i<=1;i++){
            DataSource dataSource = druidDataSource(sourcesArrayList.get(i));
            dataSources.put(String.valueOf(i),dataSource);
            if(i==0){
                super.setDefaultTargetDataSource(dataSource);
            }
        }
        super.setTargetDataSources(dataSources);
    }
    /**
     * 此处数据库信配置,可以来源于redis等,然后再初始化所有数据源
     * 重点说明:一个DruidDataSource数据源,它里面本身就是线程池了,
     * 所以我们不需要考虑线程池的问题
     * @param
     * @return
     */
    public DataSource druidDataSource(DataSources dataSources) {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(dataSources.getUrl());
        datasource.setUsername(dataSources.getUsername());
        datasource.setPassword(dataSources.getPassword());
        datasource.setDriverClassName("com.mysql.jdbc.Driver");
        datasource.setInitialSize(5);
        datasource.setMinIdle(5);
        datasource.setMaxActive(20);
        datasource.setDbType("mysql");
        datasource.setMaxWait(60000);
        datasource.setTimeBetweenEvictionRunsMillis(60000);
        datasource.setMinEvictableIdleTimeMillis(300000);
        datasource.setValidationQuery("SELECT 1 FROM DUAL");
        datasource.setTestWhileIdle(true);
        datasource.setTestOnBorrow(false);
        datasource.setTestOnReturn(false);
        try {
            datasource.setFilters("stat,wall,log4j");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return datasource;
    }

}